'use strict';

/**
 * @name PasswordModelCtrl
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('PasswordModelCtrl', ['$scope', '$stateParams', '$rootScope', '$state', 'params', 'MetaResources', 'MetaRemind', '$uibModalInstance', function($scope, $stateParams, $rootScope, $state, params, MetaResources, MetaRemind, $uibModalInstance) {
    $scope.data = {
      "old_password":"",
      "new_password":"",
      "repeat_password":""
    };

    $scope.save = function() {
        if ($scope.data.old_password!=="" && $scope.data.new_password===$scope.data.repeat_password) {
          MetaResources.user.updatePassword({
                  'id': params.data.id
              }, {'old_password': $scope.data.old_password, 'new_password': $scope.data.new_password},
              function() {
                  MetaRemind.success("恭喜", "保存成功");
                  $state.reload();
              },
              function(error) {
                  MetaRemind.serverError(error);
              });
          $uibModalInstance.dismiss('cancel');
        } else{
          MetaRemind.error("修改失败", "旧密码不得为空，或者重复密码必须与新密码一致");
        }

    };

    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };

}]);
